/*
 *    Copyright 2010-2013 the original author or authors.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package org.mybatis.jpetstore.web.actions;

import org.mybatis.jpetstore.domain.Account;
import org.mybatis.jpetstore.domain.Product;
import org.mybatis.jpetstore.service.AccountService;
import org.mybatis.jpetstore.service.CatalogService;
import org.mybatis.jpetstore.web.bean.AccountBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author Eduardo Macarron
 */
@SessionAttributes("accountBean")
@Controller
@RequestMapping("/account")
public class AccountController {

	private static final long serialVersionUID = 5499663666155758178L;

	private static final String NEW_ACCOUNT = "/account/NewAccountForm";
	private static final String EDIT_ACCOUNT = "/account/EditAccountForm";
	private static final String SIGNON = "/account/SignonForm";

	private static final List<String> LANGUAGE_LIST;
	private static final List<String> CATEGORY_LIST;

	@Autowired
	private AccountService accountService;
	@Autowired
	private CatalogService catalogService;

	static {
		List<String> langList = new ArrayList<String>();
		langList.add("english");
		langList.add("japanese");
		LANGUAGE_LIST = Collections.unmodifiableList(langList);

		List<String> catList = new ArrayList<String>();
		catList.add("FISH");
		catList.add("DOGS");
		catList.add("REPTILES");
		catList.add("CATS");
		catList.add("BIRDS");
		CATEGORY_LIST = Collections.unmodifiableList(catList);
	}

	@ModelAttribute("languages")
	public List<String> getLanguages() {
		return LANGUAGE_LIST;
	}

	@ModelAttribute("categories")
	public List<String> getCategories() {
		return CATEGORY_LIST;
	}

	@RequestMapping("newAccountForm")
	public String newAccountForm(@ModelAttribute("accountBean")AccountBean accountBean, ModelMap model) {
		return (NEW_ACCOUNT);
	}

	@RequestMapping("newAccount")
	public String newAccount(@ModelAttribute("accountBean") AccountBean accountBean) {
		accountService.insertAccount(accountBean.getAccount());
		Account account = accountService.getAccount(accountBean.getUsername());
		accountBean.setAccount(account);
		List<Product> myList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());
		accountBean.setMyList(myList);
		boolean authenticated = true;
		accountBean.setAuthenticated(authenticated);
		return "redirect:/catalog/list";
	}

	@RequestMapping("editAccountForm")
	public String editAccountForm(ModelMap model, HttpSession session) {
		model.addAttribute("accountBean", session.getAttribute("accountBean"));
		return (EDIT_ACCOUNT);
	}

	@RequestMapping(value = "editAccount", method = RequestMethod.POST)
	public String editAccount(@ModelAttribute AccountBean accountBean, ModelMap model) {
		accountService.updateAccount(accountBean.getAccount());
		Account account = accountService.getAccount(accountBean.getUsername());
		List<Product> myList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());
		return "redirect:/catalog/list";
	}

	@RequestMapping({"/", "/signonForm"})
	public String signonForm() {
		return (SIGNON);
	}

	@RequestMapping("signon")
	public String signon(String username, String password, ModelMap model, HttpSession session) {

		Account account = accountService.getAccount(username, password);

		if (account == null) {
			String message = "Invalid username or password.  Signon failed.";
			model.addAttribute("message", message);
			return (SIGNON);
		} else {
			account.setPassword(null);
			List<Product> myList = catalogService.getProductListByCategory(account.getFavouriteCategoryId());
			boolean authenticated = true;
			// this bean is already registered as /actions/Account.action
			AccountBean accountBean = new AccountBean(account, authenticated, myList);
			session.setAttribute("accountBean", accountBean);
			return "redirect:/catalog/list";
		}
	}

	@RequestMapping("signoff")
	public String signoff(HttpSession session) {
		session.invalidate();
		return "redirect:/catalog/list";
	}

}
